<template>
  <a-modal
    :visible="show"
    width="80vw"
    min-height="60vh"
    title="预算采购项选择器"
    @ok="handleOk"
    @cancel="handleCancle"
    :maskClosable="false"
    :key="show"
  >
    <div class="table-page-search-wrapper">
      <a-form-model ref="ruleForm" :laba-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="handleSearch">
        <div style="display: flex; justify-content: space-between">
          <div style="display: flex; justify-content: flex-start">
            <a-form-model-item label="预算分类:">
              <a-input v-model="searchForm.budgetType" />
            </a-form-model-item>
          </div>
          <div style="display: flex; justify-content: flex-end">
            <a-button type="primary" @click="handleSearch">查询</a-button>
          </div>
        </div>
      </a-form-model>
    </div>
    <!-- 查询区域-END -->
    <div class="table-operator"></div>
    <div class="container">
      <a-table
        ref="table"
        bordered
        size="middle"
        :scroll="{ x: true }"
        rowKey="id"
        :columns="columns"
        :dataSource="records"
        :pagination="false"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :loading="loading"
      >
        <template slot="applyNum" slot-scope="text, record">
          <a-input type="number" v-model="record.applyNum" />
        </template>
      </a-table>
      <a-pagination
        show-size-changer
        :default-current="1"
        :total="searchForm.total"
        @change="pageChange"
        @showSizeChange="onShowSizeChange"
        :show-total="(total) => `共 ${total} 条记录`"
      />
    </div>
  </a-modal>
</template>
<script>
import { postAction } from '@/api/manage'
import ExcelPaste from '@/components/excelPaste/ExcelPaste'
export default {
  components: { ExcelPaste },
  name: 'engineeBudgetSelector',
  data() {
    return {
      show: false,
      description: '工程项目预算表',
      searchForm: {
        offset: 0,
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
      },
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: "60px",
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '预算分类',
          align: 'center',
          width: '200px',
          dataIndex: 'budgetType',
          customCell: () => {
            return {
              style: {
                width: '200px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
                cursor: 'pointer',
              },
            }
          },
          customRender: (text, record) => (
            <a-tooltip placement="topLeft" title={record.budgetType}>
              {record.budgetType}
            </a-tooltip>
          ),
        },
        {
          title: '预算名称',
          align: 'center',
          width: '200px',
          dataIndex: 'budgetItem',
          customCell: () => {
            return {
              style: {
                width: '200px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
                cursor: 'pointer',
              },
            }
          },
          customRender: (text, record) => (
            <a-tooltip placement="topLeft" title={record.budgetItem}>
              {record.budgetItem}
            </a-tooltip>
          ),
        },
        {
          title: '单位',
          align: 'center',
          width: '80px',
          dataIndex: 'unit',
          customCell: () => {
            return {
              style: {
                width: '200px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
                cursor: 'pointer',
              },
            }
          },
          customRender: (text, record) => (
            <a-tooltip placement="topLeft" title={record.budgetItem}>
              {record.budgetItem}
            </a-tooltip>
          ),
        },
        {
          title: '预算总数量',
          align: 'center',
          width: '160px',
          dataIndex: 'num',
          customCell: () => {
            return {
              style: {
                width: '160px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
                cursor: 'pointer',
              },
            }
          },
          customRender: (text, record) => (
            <a-tooltip placement="topLeft" title={record.num}>
              {record.num}
            </a-tooltip>
          ),
        },
        {
          title: '累计已申请数量',
          align: 'center',
          width: '180px',
          dataIndex: 'allApplyNum',
          customCell: () => {
            return {
              style: {
                width: '180px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
                cursor: 'pointer',
              },
            }
          },
          customRender: (text, record) => (
            <a-tooltip placement="topLeft" title={record.allApplyNum}>
              {record.allApplyNum}
            </a-tooltip>
          ),
        },
        {
          title: '剩余数量',
          align: 'center',
          width: '120px',
          dataIndex: 'lessNum',
          customCell: () => {
            return {
              style: {
                width: '120px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
                cursor: 'pointer',
              },
            }
          },
          customRender: (text, record) => (
            <a-tooltip placement="topLeft" title={record.num - record.allApplyNum}>
              {record.num - record.allApplyNum}
            </a-tooltip>
          ),
        },
        {
          title: '本次申请数量',
          align: 'center',
          width: "160px",
          scopedSlots: { customRender: 'applyNum' },
        },
      ],
      url: {
        list: '/projectBudgetEnginee/list',
      },
      records: [],
      showSearchForm: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      saveDisabled: false,
      selectedRowKeys: [],
      selectedRows: [],
      loading: false,
    }
  },
  mounted() {},
  computed: {},
  methods: {
    openInit(projectBuildId, applyId) {
      if (projectBuildId) {
        this.$set(this.searchForm, 'projectBuildId', projectBuildId)
        this.$set(this.searchForm, 'applyId', applyId)
        this.handleSearch()
        this.show = true
      }
    },
    fetchList() {
      postAction(this.url.list, this.searchForm).then((res) => {
        if (res.success) {
          this.records = res.result.records
          this.$set(this.searchForm, 'total', res.result.total)
        } else {
          this.$message.error('加载数据失败:' + res.message)
        }
      })
    },
    showLength(item, len) {
      if (!item || item.length <= len) {
        return item
      }
      return item.substring(0, len) + '...'
    },
    onSelectChange(selectedRowKeys, selectedRow) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRow
    },
    handleSearch() {
      this.$set(this.searchForm, 'current', 1)
      this.fetchList()
    },
    pageChange(current, pageSize) {
      this.$set(this.searchForm, 'pageSize', pageSize)
      this.$set(this.searchForm, 'current', current)
      this.fetchList()
    },
    onShowSizeChange(current, pageSize) {
      this.$set(this.searchForm, 'pageSize', pageSize)
      this.$set(this.searchForm, 'current', 1)
      this.fetchList()
    },
    handleOk() {
      if (!this.selectedRows || this.selectedRows.length <= 0) {
        return this.$message.warning('请至少选择一条记录')
      } else {
        let index = 1
        for (let row of this.selectedRows) {
          if (!row.applyNum) {
            return this.$message.warning('请在行[' + index + ']输入本次申请数量')
          }
          if (row.applyNum > row.num - row.allApplyNum) {
            return this.$message.warning('行[' + index + ']输入的本次申请数量不能大于剩余数量')
          }
          index++
        }
      }
      this.show = false
      this.$emit('selected', this.selectedRows)
    },
    handleCancle() {
      this.show = false
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
@import '~@assets/less/hte-main.less';
@media (max-width: 1170px) {
  .page-main .container section .list-content > div.flex-num1-box {
    flex-wrap: wrap;
    > div {
      &:nth-child(1),
      &:nth-child(2) {
        width: 48%;
      }
      &:nth-child(3) {
        width: 100%;
        justify-content: flex-start;
        > span {
          margin-right: 20px;
          margin-left: 0;
        }
      }
    }
  }
  .page-main .container section .list-content > div.flex-num2-box {
    display: block;
    margin-top: 0;
  }
}
</style>